package movies
{
	import flash.filesystem.File;
	[RemoteClass(alias="movies.Movie")]
	public class Movie
	{
		protected var _urls:Array;
		protected var _name:String;
		public function Movie(url:String = null)
		{
			if(url != null)
			{
				_urls = new Array(url);
				name = (new File(url)).name;
			}else
			{
				_urls = new Array();
				name = "no name";
			}

		}
		public function get online():Boolean
		{
			var i:int = 0;
			var f:File;
			var result:Boolean = false;
			for(; i<_urls.length; i++)
			{
				f = new File(_urls[i]);
				result ||= f.exists;	
			}
			return result;
		}
		public function get exists():Boolean
		{
			if(!online)
			{
				var i:int = 0;
				var f:File;
				var result:Boolean = false;
				for(; i<_urls.length; i++)
				{
					f = new File(_urls[i]);
					while(f!=null && !f.exists)
					{
						f = f.parent;
					}
					//parent disconnected, we consider like existing
					if(f==null)
					{
						return true;
					}
				}
			}else
			{
				return true;
			}
			//if no parent is disconnected and no file corresponding = non existing
			return false;
		}
		public function containsURL(url:String):Boolean
		{
			return _urls.indexOf(url)>-1;
		}
		public function addURL(url:String):void
		{
			if(!containsURL(url))
			{
				urls = urls.concat(new Array(url));
			}
		}
		[Bindable]
		public function get urls():Array
		{
			return _urls;
		}
		public function set urls(value:Array):void
		{
			_urls = value;
			name = _urls.length > 0 ? (new File(_urls[0])).name : "no name";
		}
		[Bindable]
		public function get name():String
		{
			return _name;
		}
		public function set name(value:String):void
		{
			if(value.lastIndexOf(".")==value.length-4)
			{
				_name = value.substring(0,value.lastIndexOf("."));
			}else
			{
				_name = value;
			}
			
		}
		public function toString():String
		{
			return "[Movie name=\""+_name+"\"]";
		}

	}
}